package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type (
	GetGroupInfoReq struct {
		GroupIdList    []string `json:"GroupIdList"`
		ResponseFilter struct {
			GroupBaseInfoFilter []string `json:"GroupBaseInfoFilter"`
		} `json:"ResponseFilter"`
	}

	GetGroupInfoResp struct {
		ActionStatus string `json:"ActionStatus"`
		ErrorCode    int    `json:"ErrorCode"`
		ErrorInfo    string `json:"ErrorInfo"`
		GroupInfo    []struct {
			ErrorCode int    `json:"ErrorCode"`
			ErrorInfo string `json:"ErrorInfo"`
			GroupId   string `json:"GroupId"`
		} `json:"GroupInfo"`
	}
)

func (s sdk) GetGroupInfo(req GetGroupInfoReq) (bool, GetGroupInfoResp) {
	resp := GetGroupInfoResp{}

	reqUrl := s.BuildRestfulUrl("/group_open_http_svc/get_group_info")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("GetGroupInfo error: ", err)
		return false, resp
	}

	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")

	err = json.Unmarshal(result, &resp)

	if err != nil {
		global.Log.Error("GetGroupInfo Unmarshal error: ", err)
		return false, resp
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("GetGroupInfo error: ", err, "error message: ", resp.ErrorInfo)
		return false, resp
	}

	if resp.GroupInfo[0].ErrorCode == 0 {
		return true, resp
	}
	return false, resp
}
